package com.zzyl.serve.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.ai.AIModelInvoker;
import com.zzyl.common.ai.BaiduAIProperties;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.IDCardUtils;
import com.zzyl.common.utils.PDFUtil;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.oss.AliyunOSSOperator;
import com.zzyl.serve.domain.Elder;
import com.zzyl.serve.dto.HealthAssessmentDto;
import com.zzyl.serve.service.IElderService;
import com.zzyl.serve.vo.health.HealthReportVo;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDFTemplateBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.HealthAssessmentMapper;
import com.zzyl.serve.domain.HealthAssessment;
import com.zzyl.serve.service.IHealthAssessmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

import static com.zzyl.common.constant.RedisConstants.HEALTH_ASSESSMENT_KEY;


/**
 * 健康评估Service业务层处理
 * 
 * @author itheima
 * @date 2025-05-04
 */
@Service
public class HealthAssessmentServiceImpl extends ServiceImpl<HealthAssessmentMapper, HealthAssessment> implements IHealthAssessmentService
{
    @Autowired
    private HealthAssessmentMapper healthAssessmentMapper;
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private AIModelInvoker aiModelInvoker;
    @Autowired
    private IElderService elderService;
    /**
     * 查询健康评估
     * 
     * @param id 健康评估主键
     * @return 健康评估
     */
    @Override
    public HealthAssessment selectHealthAssessmentById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询健康评估列表
     * 
     * @param healthAssessment 健康评估
     * @return 健康评估
     */
    @Override
    public List<HealthAssessment> selectHealthAssessmentList(HealthAssessment healthAssessment)
    {
        return healthAssessmentMapper.selectHealthAssessmentList(healthAssessment);
    }

    /**
     * 新增健康评估
     * 
     * @return 结果
     */
    @Override
    public Long insertHealthAssessment(HealthAssessmentDto healthAssessmentDto)
    {
        //首先我拿到的有体检对象的名字、体检对象的身份证号、体检对象的体检中心名称、体检报告的url
        //同时，在体检报告上传的时候，redis里还存储了体检报告的string版
        //1.拿到提交报告的纯字符串版
        //老人身份证号
        String idCard = healthAssessmentDto.getIdCard();
        String text = (String) redisTemplate.opsForValue().get(HEALTH_ASSESSMENT_KEY + idCard);
        //这里需要加个判断，如果redis里面体检报告为空，需要提示给前端
        if(StringUtils.isEmpty(text)) {
            throw new RuntimeException("体检报告未上传成功，请重新上传");
        }
        //2.设置Prompt
        String prompt = "请以一个专业医生的视角来分析这份体检报告，报告中包含了一些异常数据，我需要您对这些数据进行解读，并给出相应的健康建议。\n" +
                "体检内容如下：\n" +
                text +
                "\n" +
                "要求：\n" +
                "1. 提取体检报告中的“总检日期”；\n" +
                "2. 通过临床医学、疾病风险评估模型和数据智能分析，给该用户的风险等级和健康指数给出结果。风险等级分为：健康、提示、风险、危险、严重危险。健康指数范围为0至100分；\n" +
                "3. 根据用户身体各项指标数据，详细说明该用户各项风险等级的占比是多少，最多保留两位小数。结论格式：该用户健康占比20.00%，提示占比20.00%，风险占比20%，危险占比20%，严重危险占比20%；\n" +
                "4. 对于体检报告中的异常数据，请列出（异常数据的结论、体检项目名称、检查结果、参考值、单位、异常解读、建议）这8字段。解读异常数据，解决这些数据可能代表的健康问题或风险。分析可能的原因，包括但不限于生活习惯、饮食习惯、遗传因素等。基于这些异常数据和可能的原因，请给出具体的健康建议，包括饮食调整、运动建议、生活方式改变以及是否需要进一步检查或治疗等。\n" +
                "结论格式：异常数据的结论：肥胖，体检项目名称：体重指数BMI，检查结果：29.2，参考值>24，单位：-。异常解读：体重超标包括超重与肥胖。体重指数（BMI）=体重（kg）/身⾼（m）的平⽅，BMI≥24为超重，BMI≥28为肥胖；男性腰围≥90cm和⼥性腰围≥85cm为腹型肥胖。体重超标是⼀种由多因素（如遗传、进⻝油脂较多、运动少、疾病等）引起的慢性代谢性疾病，尤其是肥胖，已经被世界卫⽣组织列为导致疾病负担的⼗⼤危险因素之⼀。AI建议：采取综合措施预防和控制体重，积极改变⽣活⽅式，宜低脂、低糖、⾼纤维素膳⻝，多⻝果蔬及菌藻类⻝物，增加有氧运动。若有相关疾病（如⾎脂异常、⾼⾎压、糖尿病等）应积极治疗。\n" +
                "5. 根据这个体检报告的内容，分别给人体的8大系统打分，每项满分为100分，8大系统分别为：呼吸系统、消化系统、内分泌系统、免疫系统、循环系统、泌尿系统、运动系统、感官系统\n" +
                "6. 给体检报告做一个总结，总结格式：体检报告中尿蛋⽩、癌胚抗原、⾎沉、空腹⾎糖、总胆固醇、⽢油三酯、低密度脂蛋⽩胆固醇、⾎清载脂蛋⽩B、动脉硬化指数、⽩细胞、平均红细胞体积、平均⾎红蛋⽩共12项指标提示异常，尿液常规共1项指标处于临界值，⾎脂、⾎液常规、尿液常规、糖类抗原、⾎清酶类等共43项指标提示正常，综合这些临床指标和数据分析：肾脏、肝胆、⼼脑⾎管存在隐患，其中⼼脑⾎管有“⾼危”⻛险；肾脏部位有“中危”⻛险；肝胆部位有“低危”⻛险。\n" +
                "\n" +
                "输出要求：\n" +
                "最后，将以上结果输出为JSON格式，不要包含其他的文字说明，所有的返回结果都是json，详细格式如下：\n" +
                "\n" +
                "{\n" +
                "  \"totalCheckDate\": \"YYYY-MM-DD\",\n" +
                "  \"healthAssessment\": {\n" +
                "    \"riskLevel\": \"健康/警告/风险/危险/严重危险\",\n" +
                "    \"healthIndex\": XX.XX\n" +
                "  },\n" +
                "  \"riskDistribution\": {\n" +
                "    \"healthy\": XX.XX,\n" +
                "    \"caution\": XX.XX,\n" +
                "    \"risk\": XX.XX,\n" +
                "    \"danger\": XX.XX,\n" +
                "    \"severeDanger\": XX.XX\n" +
                "  },\n" +
                "  \"abnormalData\": [\n" +
                "    {\n" +
                "      \"conclusion\": \"异常数据的结论\",\n" +
                "      \"examinationItem\": \"体检项目名称\",\n" +
                "      \"result\": \"检查结果\",\n" +
                "      \"referenceValue\": \"参考值\",\n" +
                "      \"unit\": \"单位\",\n" +
                "      \"interpret\":\"对于异常的结论进一步详细的说明\",\n" +
                "      \"advice\":\"针对于这一项的异常，给出一些健康的建议\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"systemScore\": {\n" +
                "    \"breathingSystem\": XX,\n" +
                "    \"digestiveSystem\": XX,\n" +
                "    \"endocrineSystem\": XX,\n" +
                "    \"immuneSystem\": XX,\n" +
                "    \"circulatorySystem\": XX,\n" +
                "    \"urinarySystem\": XX,\n" +
                "    \"motionSystem\": XX,\n" +
                "    \"senseSystem\": XX\n" +
                "  },\n" +
                "  \"summarize\": \"体检报告的总结\"\n" +
                "}";
        //3.把设置好的Prompt交给大模型进行分析
        String res = aiModelInvoker.qianfanInvoker(prompt);
        //返回值好像是json格式 给他转换成封装的VO
        HealthReportVo healthReportVo = JSONUtil.toBean(res, HealthReportVo.class);
        //4.开始分析res里面的值，并且准备给他存到数据库
        //4.1先设置一下非AI生成的字段
        HealthAssessment healthAssessment = new HealthAssessment();
        //老人姓名
        healthAssessment.setElderName(healthAssessmentDto.getElderName());
        //老人身份证号
        healthAssessment.setIdCard(idCard);
        //出生日期
        healthAssessment.setBirthDate(IDCardUtils.getBirthDateByIdCard(idCard));
        //年龄
        healthAssessment.setAge(IDCardUtils.getAgeByIdCard(idCard));
        //性别
        healthAssessment.setGender(IDCardUtils.getGenderFromIdCard(idCard));
        //体检机构
        healthAssessment.setPhysicalExamInstitution(healthAssessmentDto.getPhysicalExamInstitution());
        //体检报告的URL
        healthAssessment.setPhysicalReportUrl(healthAssessmentDto.getPhysicalReportUrl());
        //评估时间
        healthAssessment.setAssessmentTime(LocalDateTime.now());
        //4.2设置AI生成的字段
        //总检日期
        healthAssessment.setTotalCheckDate(healthReportVo.getTotalCheckDate());
        //报告总结
        healthAssessment.setReportSummary(healthReportVo.getSummarize());
        //4.2.1 HealthAssessmentVo
        //健康风险等级
        healthAssessment.setRiskLevel(healthReportVo.getHealthAssessment().getRiskLevel());
        //健康指数
        healthAssessment.setHealthScore(healthReportVo.getHealthAssessment().getHealthIndex() + "");
        //4.2.2 RiskDistributionVo
        //疾病风险分布
        healthAssessment.setDiseaseRisk(JSONUtil.toJsonStr(healthReportVo.getRiskDistribution()));
        //4.2.3 List<AbnormalDataVo>
        healthAssessment.setAbnormalAnalysis(JSONUtil.toJsonStr(healthReportVo.getAbnormalData()));
        //4.2.4 SystemScore
        //健康系统分值
        healthAssessment.setSystemScore(JSONUtil.toJsonStr(healthReportVo.getSystemScore()));
        //4.2.5
        //入住情况
        Elder elder = elderService.getOne(new LambdaQueryWrapper<Elder>()
                .eq(Elder::getIdCardNo, idCard)
                .eq(Elder::getStatus, 1));
        if(ObjectUtils.isEmpty(elder)) {
            healthAssessment.setAdmissionStatus(1);
        }
        else{
            healthAssessment.setAdmissionStatus(0);
        }
        //推荐护理等级 下面抄了个方法
        healthAssessment.setNursingLevelName(getNursingLevelName(Double.parseDouble(healthAssessment.getHealthScore())));
        //是否建议入住
        if(Double.parseDouble(healthAssessment.getHealthScore()) >= 60) {
            healthAssessment.setSuggestionForAdmission(0);
        }
        else{
            healthAssessment.setSuggestionForAdmission(1);
        }
        //保存，然后返回ID给前端
        this.save(healthAssessment);
        //保存成功后就把redis存的体检报告文本删除掉了
        redisTemplate.delete(HEALTH_ASSESSMENT_KEY + idCard);
        return healthAssessment.getId();
    }

    /**
     * 根据分数获取护理等级
     * @param healthScore
     * @return
     */
    private String getNursingLevelName(double healthScore) {
        if(healthScore<0 || healthScore>100){
            throw new BaseException("健康评分不在0-100之间");
        }
        if (healthScore>=90) {
            return "三级护理";
        }else if (healthScore>=80){
            return "二级护理";
        }else if (healthScore>=70){
            return "一级护理";
        }else if(healthScore>=60){
            return "特级护理";
        }
        return "不建议入住";
    }

    /**
     * 修改健康评估
     * 
     * @param healthAssessment 健康评估
     * @return 结果
     */
    @Override
    public int updateHealthAssessment(HealthAssessment healthAssessment)
    {
        return updateById(healthAssessment) ? 1 : 0;
    }

    /**
     * 批量删除健康评估
     * 
     * @param ids 需要删除的健康评估主键
     * @return 结果
     */
    @Override
    public int deleteHealthAssessmentByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除健康评估信息
     * 
     * @param id 健康评估主键
     * @return 结果
     */
    @Override
    public int deleteHealthAssessmentById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public AjaxResult upload(String idCardNo, MultipartFile file) {
        try {
            //哦对，先把文件上传到阿里云，然后拿到文件的url地址
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            //这里要把文件存到Redis，不过存的是里面的文本内容，通过pdfbox进行存储
            String res = PDFUtil.pdfToString(file.getInputStream());
            String key = HEALTH_ASSESSMENT_KEY + idCardNo;
            redisTemplate.opsForValue().set(key, res);
            //使用ajaxResult的好处，就是他是通过hashmap进行实现的，所以可以添加返回格式
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("fileName", url);
            ajaxResult.put("url", url);
            ajaxResult.put("originalFilename", file.getOriginalFilename());
            return ajaxResult;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
